function Bits = jpgACbits(x,C)
% y = jpgACbits(x,C)
% Computes the exact number of bits to Huffman code
% an N x N quantized DCT block
% using JPEG Huffman table for AC coefficients.
% DCT coefficients are zigzag scanned and
% the RL/Amplitude pairs are found. Then,
% using the JPEG RL/Amp table, number of bits to
% compress the block is determined.
% Input:
% x = N x N quantized DCT block
% C = 'Y' for luma or 'C' for chroma
% Outputs:
% Bits = total bits to encode the block
%
% RLCy is the JPEG run-length/size-category Huffman table
% for luma AC coefficients &
% RLCc is the corresponding Huffman table for the chroma component
% Each entry in the table is the code length for the RL/size
% category which equals the RL/size Huffman code plus the
% binary code of the actual non-zero coefficient amplitude.
% RLC{1}(1) is the EOB code corresponding to 0/0
% RLC{16}(1) is the Huffman code for RL/size = 16/0
%
RLCy = cell(1,15);
RLCy{1}=int16([4 3 4 6 8 10 12 14 18 25 26]);
RLCy{2}= int16([5 8 10 13 16 22 23 24 25 26]);
RLCy{3}= int16([6 10 13 20 21 22 23 24 25 26]);
RLCy{4}= int16([7 11 14 20 21 22 23 24 25 26]);
RLCy{5}= int16([7 12 19 20 21 22 23 24 25 26]);
RLCy{6}= int16([8 12 19 20 21 22 23 24 25 26]);
RLCy{7}= int16([8 13 19 20 21 22 23 24 25 26]);
RLCy{8}= int16([9 13 19 20 21 22 23 24 25 26]);
RLCy{9}= int16([9 17 19 20 21 22 23 24 25 26]);
RLCy{10}= int16([10 18 19 20 21 22 23 24 25 26]);
RLCy{11}= int16([10 18 19 20 21 22 23 24 25 26]);
RLCy{12}= int16([10 18 19 20 21 22 23 24 25 26]);
RLCy{13}= int16([11 18 19 20 21 22 23 24 25 26]);
RLCy{14}= int16([12 18 19 20 21 22 23 24 25 26]);
RLCy{15}= int16([13 18 19 20 21 22 23 24 25 26]);
RLCy{16}= int16([12 17 18 19 20 21 22 23 24 25 26]);
%
RLCc = cell(1,15);
RLCc{1}=int16([2 3 5 7 9 12 15 23 24 25 26]);
RLCc{2}= int16([5 8 11 14 20 22 23 24 25 26]);
RLCc{3}= int16([6 9 13 15 21 22 23 24 25 26]);
RLCc{4}= int16([6 9 12 15 21 22 23 24 25 26]);
RLCc{5}= int16([6 10 14 20 21 22 23 24 25 26]);
RLCc{6}= int16([7 11 17 20 21 22 23 24 25 26]);
RLCc{7}= int16([8 12 19 20 21 22 23 24 25 26]);
RLCc{8}= int16([7 12 19 20 21 22 23 24 25 26]);
RLCc{9}= int16([8 14 19 20 21 22 23 24 25 26]);
RLCc{10}= int16([9 14 19 20 21 22 23 24 25 26]);
RLCc{11}= int16([9 14 19 20 21 22 23 24 25 26]);
RLCc{12}= int16([9 14 19 20 21 22 23 24 25 26]);
RLCc{13}= int16([9 18 19 20 21 22 23 24 25 26]);
RLCc{14}= int16([11 18 19 20 21 22 23 24 25 26]);
RLCc{15}= int16([13 18 19 20 21 22 23 24 25 26]);
RLCc{16}= int16([11 17 18 19 20 21 22 23 24 25 26]);
%
switch C
case 'Y'
RLC = RLCy;
case 'C'
RLC = RLCc;
end
x1 = ZigZag(x); % zigzag scan the 8 x 8 DCT blocks
%
k = 2; Count = 0; Bits = double(0);
while k<=64
if x1(k) == 0
Count = Count + 1;
if k == 64
Bits = Bits + double(RLC{1}(1));
break;
end
else
if Count == 0
RL = Count;
Level = round(log2(abs(x1(k)))+0.5);
Bits = Bits + double(RLC{RL+1}(Level+1));
elseif Count >=1&&Count<=15
RL = Count;
Level = round(log2(abs(x1(k)))+0.5);
Bits = Bits + double(RLC{RL+1}(Level));
Count = 0;
else
Bits = Bits + double(RLC{16}(1));
Count = Count - 16;
end
end
k=k+1;
end
